package events
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	public class GameDispatherEvent extends EventDispatcher implements IEventDispatcher
	{
		private static var instance:GameDispatherEvent;
		
		private static var eventMap:Object;
		public function GameDispatherEvent(target:IEventDispatcher=null)
		{
			instance = this;
			super(target);
			eventMap={};
		}
		
		public static function getInstance():void{
			instance = new GameDispatherEvent();
		}
		
		public static function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void{
			instance.addEventListener(type,listener,useCapture,priority,useWeakReference);
		}
		
		public static function removeEventLister(type:String,listener:Function):void{
			instance.removeEventListener(type,listener);
		}
		
		public static function dispatchEvent(type:String,data:Object=null):void{
			var event:GameEvent;
			if(eventMap[type]&&eventMap[type].length){
				event = eventMap[type].shift();
				event.data = data;
			}else if(eventMap[type]&&!eventMap[type].length){
				event = new GameEvent(type,data);
			}else{
				event = new GameEvent(type,data);
				eventMap[type] = [];
				eventMap[type].push(event);
			}
			instance.dispatchEvent(event);
		}
	}
}